package com.miyake.loveviewweb.server.controller;

import java.util.List;

import org.slim3.controller.Controller;
import org.slim3.controller.Navigation;
import org.slim3.datastore.Datastore;

import com.google.appengine.api.datastore.Key;
import com.miyake.loveviewweb.server.meta.BinaryModelMeta;
import com.miyake.loveviewweb.server.meta.DataModelMeta;
import com.miyake.loveviewweb.server.meta.LvBlobMeta;
import com.miyake.loveviewweb.shared.model.BinaryModel;
import com.miyake.loveviewweb.shared.model.DataModel;
import com.miyake.loveviewweb.shared.model.LvBlob;

public class DataImageController extends Controller {

    @Override
    public Navigation run() throws Exception {
        if (this.isGet()) {
            Key key = Datastore.stringToKey(this.request.getParameter("key"));
            
            DataModel dataModel = Datastore.get(DataModel.class, key);
            BinaryModel binary = Datastore.get(BinaryModel.class, dataModel.getOriginalData());

            byte[] b = binary.getBlobData().getBytes();
            
            this.response.setContentType(binary.getContentType());
            this.response.getOutputStream().write(b ,0, b.length);
            this.response.getOutputStream().flush();
            this.response.getOutputStream().close();    
        }
        return null;//forward("staticChart.jsp");
    }
}
